#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>

int main(){
    const char *file_path="file.txt";
    int fd = open(file_path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
    if(fd == -1){
        perror("open");
        exit(EXIT_FAILURE);
    }

    const char* text = "Hello, World!";
    ssize_t bytes_write = write(fd, text, strlen(text));
    if(bytes_write == -1){
        perror("write");
        close(fd);
        exit(EXIT_FAILURE);
    }

    if(lseek(fd, 0, SEEK_SET) == -1){
        perror("lseek");
        close(fd);
        exit(EXIT_FAILURE);
    }
    char buffer[256];
    ssize_t bytes_read = read(fd, buffer, sizeof(buffer));
    if(bytes_read == -1){
        perror("read");
        close(fd);
        exit(EXIT_FAILURE);
    }
    buffer[bytes_read]='\0';
    printf("读取的数据:%s\n", buffer);
    if(close(fd) == -1){
        perror("close");
        exit(EXIT_FAILURE);
    }
    return 0;
}